// 生成随机数据的工具函数
const generateRandomInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
const generateRandomFloat = (min, max, decimals = 2) => 
  parseFloat((Math.random() * (max - min) + min).toFixed(decimals))

// 生成日期范围
const generateDateRange = (days = 7) => {
  const dates = []
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date()
    date.setDate(date.getDate() - i)
    dates.push({
      date: date.toISOString().split('T')[0],
      label: date.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' })
    })
  }
  return dates
}

// 生成时间序列数据
const generateHourlyData = () => {
  return Array.from({ length: 24 }, (_, i) => ({
    hour: i,
    label: `${i.toString().padStart(2, '0')}:00`,
    value: generateRandomInRange(500, 2000) + Math.sin(i / 24 * Math.PI * 2) * 300
  }))
}

// 核心业务指标
export const coreMetrics = {
  todaySales: {
    value: 25800000,
    change: 12.5,
    trend: 'up'
  },
  orders: {
    value: 15680,
    change: 8.3,
    trend: 'up'
  },
  activeUsers: {
    value: 89240,
    change: 15.2,
    trend: 'up'
  },
  conversionRate: {
    value: 3.67,
    change: 0.23,
    trend: 'up'
  },
  avgOrderValue: {
    value: 164.5,
    change: -2.1,
    trend: 'down'
  }
}

// 实时数据流
export const realTimeData = {
  pv: generateRandomInRange(1200000, 1300000),
  uv: generateRandomInRange(230000, 250000),
  orders: generateRandomInRange(15000, 16000),
  revenue: generateRandomFloat(12, 13, 2),
  refundRate: generateRandomFloat(1.0, 1.5, 1)
}

// 实时订单流数据
export const realTimeOrders = []

// 生成实时订单的工具函数
const userNames = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十', '郑十一', '王十二', '冯十三', '陈十四', '楚十五', '魏十六', '蒋十七']
const products = ['iPhone 15 Pro', '华为Mate60', '小米14', 'MacBook Pro', 'iPad Air', '华为手表', '小米耳机', 'AirPods Pro', '戴森吸尘器', '海尔洗衣机']
const cities = ['北京', '上海', '广州', '深圳', '杭州', '成都', '重庆', '西安', '武汉', '南京']

export function generateRealtimeOrder() {
  const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#96ceb4', '#feca57', '#ff9ff3', '#54a0ff', '#5f27cd']
  
  return {
    id: Date.now() + Math.random(),
    user: userNames[Math.floor(Math.random() * userNames.length)],
    product: products[Math.floor(Math.random() * products.length)],
    amount: (Math.random() * 5000 + 500).toFixed(0),
    location: cities[Math.floor(Math.random() * cities.length)],
    timeAgo: '刚刚',
    avatarColor: colors[Math.floor(Math.random() * colors.length)],
    timestamp: Date.now()
  }
}

// 初始化一些订单数据
for (let i = 0; i < 10; i++) {
  const order = generateRealtimeOrder()
  order.timestamp = Date.now() - i * 30000 // 每30秒一个
  realTimeOrders.push(order)
}

// 热销商品排行
export const hotProducts = [
  {
    name: 'iPhone 15 Pro Max',
    sales: 2580,
    revenue: 25800000,
    category: '手机',
    image: '/images/iphone.jpg',
    growth: 15.6
  },
  {
    name: 'MacBook Air M3',
    sales: 1890,
    revenue: 18900000,
    category: '电脑',
    image: '/images/macbook.jpg',
    growth: 12.3
  },
  {
    name: 'iPad Pro 12.9"',
    sales: 1650,
    revenue: 13200000,
    category: '平板',
    image: '/images/ipad.jpg',
    growth: 8.9
  },
  {
    name: 'AirPods Pro 2',
    sales: 1420,
    revenue: 3408000,
    category: '音频',
    image: '/images/airpods.jpg',
    growth: 22.1
  },
  {
    name: 'Apple Watch Ultra',
    sales: 1280,
    revenue: 10240000,
    category: '可穿戴',
    image: '/images/watch.jpg',
    growth: 18.7
  },
  {
    name: 'Nike Air Max 270',
    sales: 1150,
    revenue: 1725000,
    category: '运动鞋',
    image: '/images/nike.jpg',
    growth: 5.2
  },
  {
    name: '小米13 Ultra',
    sales: 980,
    revenue: 5880000,
    category: '手机',
    image: '/images/xiaomi.jpg',
    growth: 11.4
  },
  {
    name: 'Dell XPS 13',
    sales: 850,
    revenue: 8500000,
    category: '电脑',
    image: '/images/dell.jpg',
    growth: 7.8
  },
  {
    name: 'Samsung Galaxy S24',
    sales: 720,
    revenue: 5760000,
    category: '手机',
    image: '/images/samsung.jpg',
    growth: 9.6
  },
  {
    name: 'Sony WH-1000XM5',
    sales: 650,
    revenue: 2600000,
    category: '音频',
    image: '/images/sony.jpg',
    growth: 14.2
  },
  {
    name: 'Tesla Model Y',
    sales: 580,
    revenue: 23200000,
    category: '汽车',
    image: '/images/tesla.jpg',
    growth: 25.8
  },
  {
    name: '华为MatePad Pro',
    sales: 520,
    revenue: 2600000,
    category: '平板',
    image: '/images/huawei.jpg',
    growth: 13.2
  },
  {
    name: 'Dyson V15 Detect',
    sales: 480,
    revenue: 2400000,
    category: '家电',
    image: '/images/dyson.jpg',
    growth: 19.5
  },
  {
    name: 'Nintendo Switch OLED',
    sales: 450,
    revenue: 1350000,
    category: '游戏',
    image: '/images/switch.jpg',
    growth: 8.7
  },
  {
    name: 'Adidas Yeezy 350',
    sales: 420,
    revenue: 1260000,
    category: '运动鞋',
    image: '/images/yeezy.jpg',
    growth: 16.3
  },
  {
    name: 'Canon EOS R5',
    sales: 380,
    revenue: 1140000,
    category: '相机',
    image: '/images/canon.jpg',
    growth: 7.9
  },
  {
    name: 'LV Neverfull MM',
    sales: 350,
    revenue: 1750000,
    category: '奢侈品',
    image: '/images/lv.jpg',
    growth: 12.1
  },
  {
    name: 'Bose QC45',
    sales: 320,
    revenue: 960000,
    category: '音频',
    image: '/images/bose.jpg',
    growth: 14.6
  },
  {
    name: 'GoPro Hero 12',
    sales: 290,
    revenue: 1160000,
    category: '相机',
    image: '/images/gopro.jpg',
    growth: 21.3
  },
  {
    name: 'Rolex Submariner',
    sales: 260,
    revenue: 2600000,
    category: '奢侈品',
    image: '/images/rolex.jpg',
    growth: 9.8
  },
  {
    name: 'Xiaomi Air Purifier',
    sales: 240,
    revenue: 240000,
    category: '家电',
    image: '/images/xiaomi-air.jpg',
    growth: 18.4
  },
  {
    name: 'Lego Technic Porsche',
    sales: 220,
    revenue: 440000,
    category: '玩具',
    image: '/images/lego.jpg',
    growth: 6.2
  },
  {
    name: 'Beats Studio Pro',
    sales: 200,
    revenue: 600000,
    category: '音频',
    image: '/images/beats.jpg',
    growth: 11.7
  },
  {
    name: 'DJI Mini 4 Pro',
    sales: 180,
    revenue: 900000,
    category: '无人机',
    image: '/images/dji.jpg',
    growth: 23.9
  }
]

// 商品分类数据
export const categoryData = [
  {
    name: '电子产品',
    value: 45.6,
    count: 15420,
    revenue: 156800000,
    color: '#00f0ff'
  },
  {
    name: '服装鞋帽',
    value: 23.8,
    count: 8750,
    revenue: 52300000,
    color: '#00ff9d'
  },
  {
    name: '家居用品',
    value: 15.2,
    count: 5680,
    revenue: 34200000,
    color: '#ffa502'
  },
  {
    name: '图书文具',
    value: 8.7,
    count: 3240,
    revenue: 12400000,
    color: '#ff4757'
  },
  {
    name: '运动户外',
    value: 6.7,
    count: 2890,
    revenue: 18600000,
    color: '#7bed9f'
  }
]

// 销售趋势数据（24小时）
export const hourlyTrend = generateHourlyData()

// 用户活跃度数据（7天）
export const weeklyActivity = generateDateRange(7).map(({ date, label }) => ({
  date,
  label,
  pv: generateRandomInRange(2000, 4000),
  uv: generateRandomInRange(1500, 2500),
  orders: generateRandomInRange(100, 300),
  revenue: generateRandomInRange(50000, 150000)
}))

// 地区分布数据
export const regionData = [
  { name: '北京', value: [116.46, 39.92], users: 12580, orders: 2580, revenue: 25800000 },
  { name: '上海', value: [121.48, 31.22], users: 11200, orders: 2340, revenue: 23400000 },
  { name: '深圳', value: [114.07, 22.62], users: 9800, orders: 2156, revenue: 21560000 },
  { name: '广州', value: [113.23, 23.16], users: 8900, orders: 1980, revenue: 19800000 },
  { name: '杭州', value: [120.19, 30.26], users: 7600, orders: 1723, revenue: 17230000 },
  { name: '成都', value: [104.06, 30.67], users: 6800, orders: 1456, revenue: 14560000 },
  { name: '重庆', value: [106.54, 29.59], users: 6200, orders: 1278, revenue: 12780000 },
  { name: '武汉', value: [114.31, 30.52], users: 5500, orders: 1134, revenue: 11340000 },
  { name: '西安', value: [108.95, 34.27], users: 4800, orders: 987, revenue: 9870000 },
  { name: '南京', value: [118.78, 32.04], users: 4200, orders: 856, revenue: 8560000 }
]

// 3D销售数据（周 x 小时）
export const sales3DData = (() => {
  const days = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  const data = []
  
  for (let day = 0; day < 7; day++) {
    for (let hour = 0; hour < 24; hour++) {
      // 模拟真实的销售模式：工作日白天销量高，周末晚上销量高
      let baseValue = 300
      if (day < 5) { // 工作日
        if (hour >= 9 && hour <= 18) baseValue = 800
        if (hour >= 12 && hour <= 14) baseValue = 1200 // 午休时间
      } else { // 周末
        if (hour >= 10 && hour <= 22) baseValue = 900
        if (hour >= 19 && hour <= 21) baseValue = 1500 // 晚上购物高峰
      }
      
      const randomVariation = generateRandomInRange(-200, 200)
      data.push([day, hour, Math.max(0, baseValue + randomVariation)])
    }
  }
  
  return {
    days,
    hours: Array.from({ length: 24 }, (_, i) => `${i}时`),
    data
  }
})()

// 实时数据更新函数
export const updateRealTimeData = () => {
  realTimeData.pv += generateRandomInRange(-1000, 2000)
  realTimeData.uv += generateRandomInRange(-500, 1000)
  realTimeData.orders += generateRandomInRange(-10, 30)
  realTimeData.revenue = generateRandomFloat(realTimeData.revenue - 0.5, realTimeData.revenue + 0.8, 2)
  realTimeData.refundRate = generateRandomFloat(1.0, 1.8, 1)
  
  // 更新核心指标
  coreMetrics.todaySales.value += generateRandomInRange(-50000, 100000)
  coreMetrics.orders.value += generateRandomInRange(-5, 15)
  coreMetrics.activeUsers.value += generateRandomInRange(-100, 300)
  coreMetrics.conversionRate.value = generateRandomFloat(3.2, 4.1, 2)
  coreMetrics.avgOrderValue.value = generateRandomFloat(160, 170, 1)
  
  return {
    realTimeData,
    coreMetrics
  }
}

// 获取随机颜色
export const getRandomColor = () => {
  const colors = ['#00f0ff', '#00ff9d', '#ffa502', '#ff4757', '#7bed9f', '#3742fa', '#f368e0']
  return colors[Math.floor(Math.random() * colors.length)]
}

// 数据更新间隔（毫秒）
export const UPDATE_INTERVALS = {
  REAL_TIME: 3000,    // 实时数据3秒更新
  CHARTS: 10000,      // 图表数据10秒更新
  TRENDS: 30000       // 趋势数据30秒更新
} 